home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UNIXTOOL / M4SRC / _files / _M4 / Look._C < prev    next >
Encoding:
Text File  |  1989-10-15  |  1.6 KB  |  110 lines

  1. /*
  2.  * look.c
  3.  * Facility: m4 macro processor
  4.  * by: oz
  5.  */
  6.  
  7. #include "mdef.h"
  8. #include "extr.h"
  9.  
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. static void freent (ndptr p);
  14.  
  15. /*
  16.  *  hash - compute hash value using the proverbial
  17.  *       hashing function. Taken from K&R.
  18.  */
  19. int hash (char *name)
  20. {
  21.     register int h = 0;
  22.     while (*name)
  23.         h += *name++;
  24.     return (h % HASHSIZE);
  25. }
  26.  
  27. /*
  28.  * lookup - find name in the hash table
  29.  *
  30.  */
  31. ndptr lookup (char *name)
  32. {
  33.     register ndptr p;
  34.  
  35.     for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
  36.         if (strcmp(name, p->name) == 0)
  37.             break;
  38.     return (p);
  39. }
  40.  
  41. /*
  42.  * addent - hash and create an entry in the hash
  43.  *        table. The new entry is added in front
  44.  *        of a hash bucket.
  45.  */
  46. ndptr addent (char *name)
  47. {
  48.     register int h;
  49.     ndptr p;
  50.  
  51.     h = hash(name);
  52.     if ((p = (ndptr) malloc(sizeof(struct ndblock))) != NULL) {
  53.         p->nxtptr = hashtab[h];
  54.         hashtab[h] = p;
  55.         p->name = strsave(name);
  56.     }
  57.     else
  58.         error("M4: No more memory");
  59.     return p;
  60. }
  61.  
  62. /*
  63.  * remhash - remove an entry from the hashtable
  64.  *
  65.  */
  66. void remhash (char *name, int all)
  67. {
  68.     register int h;
  69.     register ndptr xp, tp, mp;
  70.  
  71.     h = hash(name);
  72.     mp = hashtab[h];
  73.     tp = nil;
  74.     while (mp != nil) {
  75.         if (strcmp(mp->name, name) == 0) {
  76.             mp = mp->nxtptr;
  77.             if (tp == nil) {
  78.                 freent(hashtab[h]);
  79.                 hashtab[h] = mp;
  80.             }
  81.             else {
  82.                 xp = tp->nxtptr;
  83.                 tp->nxtptr = mp;
  84.                 freent(xp);
  85.             }
  86.             if (!all)
  87.                 break;
  88.         }
  89.         else {
  90.             tp = mp;
  91.             mp = mp->nxtptr;
  92.         }
  93.     }
  94. }
  95.  
  96. /*
  97.  * freent - free a hashtable information block
  98.  *
  99.  */
  100. static void freent (ndptr p)
  101. {
  102.     if (!(p->type & STATIC)) {
  103.         free(p->name);
  104.         if (p->defn != null)
  105.             free(p->defn);
  106.     }
  107.     free(p);
  108. }
  109.  
  110.